package repository

import (
	"api-gateway-center/domain/register/model"
	"api-gateway-center/domain/register/repository"
	"api-gateway-center/infrastructure/po"
	"gorm.io/gorm"
	"time"
)

type RegisterManageRepository struct {
	db *gorm.DB
}

func NewRegisterManageRepository(db *gorm.DB) repository.RegisterManageRepository {
	return &RegisterManageRepository{db: db}
}

// RegisterApplication 注册应用程序系统
func (this *RegisterManageRepository) RegisterApplication(applicationSystemVO model.ApplicationSystemVO) error {
	applicationSystem := po.ApplicationSystem{
		SystemId:       applicationSystemVO.SystemId,
		SystemName:     applicationSystemVO.SystemName,
		SystemType:     applicationSystemVO.SystemType,
		SystemRegistry: applicationSystemVO.SystemRegistry,
		CreatedAt:      time.Now(),
		UpdatedAt:      time.Now(),
	}
	return this.db.Create(&applicationSystem).Error
}

// RegisterApplicationInterface 注册应用程序接口
func (this *RegisterManageRepository) RegisterApplicationInterface(applicationInterfaceVO model.ApplicationInterfaceVO) error {
	applicationInterface := po.ApplicationInterface{
		SystemId:         applicationInterfaceVO.SystemId,
		InterfaceId:      applicationInterfaceVO.InterfaceId,
		InterfaceName:    applicationInterfaceVO.InterfaceName,
		InterfaceVersion: applicationInterfaceVO.InterfaceVersion,
		CreatedAt:        time.Now(),
		UpdatedAt:        time.Now(),
	}
	result := this.db.Create(&applicationInterface)
	return result.Error
}

// RegisterApplicationInterfaceMethod 注册应用程序接口方法
func (this *RegisterManageRepository) RegisterApplicationInterfaceMethod(applicationInterfaceMethodVO model.ApplicationInterfaceMethodVO) error {
	applicationInterfaceMethod := po.ApplicationInterfaceMethod{
		SystemId:        applicationInterfaceMethodVO.SystemId,
		InterfaceId:     applicationInterfaceMethodVO.InterfaceId,
		MethodId:        applicationInterfaceMethodVO.MethodId,
		MethodName:      applicationInterfaceMethodVO.MethodName,
		ParameterType:   applicationInterfaceMethodVO.ParameterType,
		Uri:             applicationInterfaceMethodVO.Uri,
		HttpCommandType: applicationInterfaceMethodVO.HttpCommandType,
		Auth:            applicationInterfaceMethodVO.Auth,
		CreatedAt:       time.Now(),
		UpdatedAt:       time.Now(),
	}
	result := this.db.Create(&applicationInterfaceMethod)
	return result.Error
}
